package org.taobao.ibatiside.ui.wizard;

import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.widgets.Composite;
import org.taobao.ibatiside.model.mapperv2.Result;
import org.taobao.ibatiside.model.mapperv2.ResultMap;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;

public class ShowResultMapInfoComposite extends Composite {
	private Label lbId;
	private Label lbClass;
	private Label lblDiscri;
	private Label lbExtends;
	private Label lbGroupBy;
	private Label lbXmlName;
	private Table table;
	private TableViewer tableViewer;
	static class ResultMapLabelProvider extends LabelProvider implements ITableLabelProvider{
		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			Result result = (Result) element;
			switch (columnIndex){
			case 0:
				return result.getColumn();
			case 1:
				return String.valueOf(result.getColumnIndex());
			case 2:
				return result.getProperty();
			case 3:
				return result.getNullValue();
			case 4:
				return result.getTypeHandler();
			}
			return null;
		}
	}
	public ShowResultMapInfoComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(1, false));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setLayout(new GridLayout(6, false));
		GridData gd_composite = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_composite.heightHint = 50;
		composite.setLayoutData(gd_composite);
		
		Label lblId = new Label(composite, SWT.NONE);
		lblId.setAlignment(SWT.RIGHT);
		GridData gd_lblId = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblId.widthHint = 60;
		lblId.setLayoutData(gd_lblId);
		lblId.setText("ID");
		
		lbId = new Label(composite, SWT.NONE);
		lbId.setText("id value");
		GridData gd_lbId = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lbId.widthHint = 120;
		lbId.setLayoutData(gd_lbId);
		lbId.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		
		Label lblClass = new Label(composite, SWT.NONE);
		lblClass.setText("CLASS");
		
		lbClass = new Label(composite, SWT.NONE);
		lbClass.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		GridData gd_lbClass = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lbClass.widthHint = 150;
		lbClass.setLayoutData(gd_lbClass);
		lbClass.setText("class value");
		
		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("Discriminator");
		
		lblDiscri = new Label(composite, SWT.NONE);
		lblDiscri.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		lblDiscri.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lblDiscri.setText("New Label");
		
		Label lblExtends = new Label(composite, SWT.NONE);
		GridData gd_lblExtends = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_lblExtends.widthHint = 60;
		lblExtends.setLayoutData(gd_lblExtends);
		lblExtends.setAlignment(SWT.RIGHT);
		lblExtends.setText("Extends");
		
		lbExtends = new Label(composite, SWT.NONE);
		lbExtends.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lbExtends.setText("New Label");
		
		Label lblGroupBy = new Label(composite, SWT.NONE);
		lblGroupBy.setText("Group By");
		
		lbGroupBy = new Label(composite, SWT.NONE);
		lbGroupBy.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lbGroupBy.setText("New Label");
		
		Label lblNewLabel_1 = new Label(composite, SWT.NONE);
		lblNewLabel_1.setAlignment(SWT.RIGHT);
		lblNewLabel_1.setText("Xml Name");
		
		lbXmlName = new Label(composite, SWT.NONE);
		lbXmlName.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		lbXmlName.setText("New Label");
		
		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnColumnName = tableViewerColumn.getColumn();
		tblclmnColumnName.setWidth(112);
		tblclmnColumnName.setText("Column Name");
		
		TableViewerColumn tableViewerColumn_1 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnIndex = tableViewerColumn_1.getColumn();
		tblclmnIndex.setWidth(46);
		tblclmnIndex.setText("Index");
		
		TableViewerColumn tableViewerColumn_2 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnProperty = tableViewerColumn_2.getColumn();
		tblclmnProperty.setWidth(130);
		tblclmnProperty.setText("Property");
		
		TableViewerColumn tableViewerColumn_3 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn = tableViewerColumn_3.getColumn();
		tblclmnNewColumn.setWidth(140);
		tblclmnNewColumn.setText("Null Value");
		
		TableViewerColumn tableViewerColumn_4 = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn tblclmnNewColumn_1 = tableViewerColumn_4.getColumn();
		tblclmnNewColumn_1.setWidth(217);
		tblclmnNewColumn_1.setText("Type Handler");
		
		tableViewer.setContentProvider(new ArrayContentProvider());
		tableViewer.setLabelProvider(new ResultMapLabelProvider());
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public void setResultMap(ResultMap new_map) {
		lbId.setText(new_map.getId());
		lbClass.setText(new_map.getClass_());
		lblDiscri.setText(new_map.getDiscriminator() != null ? new_map.getDiscriminator().getColumn() : " ");
		lbExtends.setText(new_map.getExtends() != null ? new_map.getExtends().getId() : " ");
		lbGroupBy.setText(StringUtils.defaultString(new_map.getGroupBy()));
		lbXmlName.setText(StringUtils.defaultString(new_map.getXmlName()));
		tableViewer.setInput(new_map.getResult());
	}
}
